package com.cxrj.zhsq.partyMember.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.partyMember.dao.ActivityApplyMapper;
import com.cxrj.zhsq.partyMember.dao.PartyMemberActivityMapper;
import com.cxrj.zhsq.partyMember.entity.ActivityApply;
import com.cxrj.zhsq.partyMember.entity.PartyMemberActivity;
import com.cxrj.zhsq.partyMember.service.PartyMemberActivityService;
import com.cxrj.zhsq.userManage.dao.AppUserMapper;
import com.cxrj.zhsq.userManage.entity.AppUser;
import com.cxrj.zhsq.utils.SqlCondition;

@Service
public class PartyMemberActivityServiceImpl implements PartyMemberActivityService {
	
	@Resource
	private PartyMemberActivityMapper partyMemberActivityMapper;
	
	@Resource
	private ActivityApplyMapper activityApplyMapper;
	
	@Resource
	private AppUserMapper appUserMapper;

	@Override
	public Map<String, Object> selectActivityPage(HttpServletRequest request, HttpServletResponse response) {
		partyMemberActivityMapper.updateRecruiByTime();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = SqlCondition.getBaseCondition(request, response);
		sqlMap.put("activityState", request.getParameter("activityState"));
		//查询条件
		String kod=request.getParameter("kod");
		String kodType=request.getParameter("kodType");
		String sqlStr = "";
		
		if(kodType != null && !kodType.equals("")){
			sqlStr += "and activity_state = " + kodType;
		}
		
		if(kod != null && !kod.equals("")){
			//sqlStr += " and ( work_order_title like " + '"' + '%' + kod + '%' + '"'+" or work_order_people like " + '"' + '%' + kod + '%' + '"'+')';
			sqlStr += " and activity_title like " + '"' + '%' + kod + '%' + '"';
		}
		sqlMap.put("condition",sqlStr);
		int count = partyMemberActivityMapper.selectCount(sqlMap);
		List<PartyMemberActivity> list = partyMemberActivityMapper.selectActivityPage(sqlMap);
		resultMap.put("total", count);
		resultMap.put("recordsFiltered", count);
		resultMap.put("rows", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertActivity(PartyMemberActivity partyMemberActivity, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		Date date = new Date();
		partyMemberActivity.setActivityId(UUID.randomUUID().toString());
		partyMemberActivity.setActivityStateTime(date);
		partyMemberActivity.setCreateTime(date);
		int a=partyMemberActivityMapper.insertSelective(partyMemberActivity);
		resultMap.put("result", a);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectActivityById(String activityId, String userId, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		PartyMemberActivity memberActivity=partyMemberActivityMapper.selectByPrimaryKey(activityId);
		List<ActivityApply> list = new ArrayList<ActivityApply>();
		Integer isSign = 0;
		if(userId != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("activityId", activityId);
			map.put("userId", userId);
			list = activityApplyMapper.selectActivityApply(activityId);
			isSign = activityApplyMapper.selectActivityApplyBySign(map);
		}
		resultMap.put("result", memberActivity);
		resultMap.put("isSign", isSign);
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateActivity(PartyMemberActivity partyMemberActivity, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		int a=partyMemberActivityMapper.updateByPrimaryKeySelective(partyMemberActivity);
		resultMap.put("result", a);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectActivityApply(String activityId,HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		List<ActivityApply> activityApplies=activityApplyMapper.selectActivityApply(activityId);
		resultMap.put("result", activityApplies);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectActivity(String dataIndex, String pageCount) {
		partyMemberActivityMapper.updateRecruiByTime();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("start_page", Integer.parseInt(dataIndex));
		sqlMap.put("end_page", Integer.parseInt(pageCount));
		sqlMap.put("activityState", 0);
		resultMap.put("list", partyMemberActivityMapper.selectActivityPage(sqlMap));
		return resultMap;
	}

	@Override
	public Map<String, Object> insertActivityApply(String userId, String activityid) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
		ActivityApply activityApply = new ActivityApply();
		activityApply.setApplyId(UUID.randomUUID().toString());
		activityApply.setApplyUserId(userId);
		activityApply.setActivityId(activityid);
		activityApply.setApplyAddress(appUser.getUserAddress());
		activityApply.setApplyIdcard(appUser.getUserIdCard());
		activityApply.setApplyName(appUser.getUserName());
		activityApply.setApplyTel(appUser.getUserMobile());
		activityApply.setApplyTime(new Date());
		Integer c = activityApplyMapper.insertSelective(activityApply);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateActivityApply(String userId, String activityId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("userId", userId);
		sqlMap.put("activityId", activityId);
		Integer c = activityApplyMapper.updateActivityApply(sqlMap);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectActivityByMy(String userId, String status, String dataIndex, String pageCount) {
		partyMemberActivityMapper.updateRecruiByTime();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("start_page", Integer.parseInt(dataIndex));
		sqlMap.put("end_page", Integer.parseInt(pageCount));
		sqlMap.put("activityState", status);
		sqlMap.put("userId", userId);
		resultMap.put("list", partyMemberActivityMapper.selectActivityByMy(sqlMap));
		return resultMap;
	}
}
